package huffman;

import java.io.*;
import java.util.*;
import java.util.Map.*;
public class read_file {
	
	private FileInputStream fis;
	//private FileOutputStream fos;
	//private PrintStream ps;
	private HashMap<Byte,Integer> map;
	
	public read_file(){	
	}
	
	public HashMap<Byte,Integer> get_map(){
		return this.map;
	}
	
	public HashMap<Byte,Integer> read(String file_path){
		this.map=new HashMap<Byte,Integer>();
		try{
			this.fis=new FileInputStream(file_path);
		}catch(Exception e){
			e.printStackTrace();
			//System.exit(1);
		}
		
		byte[] byte_array=new byte[256];
		int len=0;
		try{
			while((len=this.fis.read(byte_array))!=-1){
				for(int i=0;i!=len;++i){
					if(this.map.containsKey(byte_array[i])){
						int cnt=this.map.get(byte_array[i]);
						++cnt;
						this.map.remove(byte_array[i]);
						this.map.put(byte_array[i], cnt);
					}else{
						this.map.put(byte_array[i], 1);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			//System.exit(1);
		}
		try{
			this.fis.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return get_map();
	}
	
	public static void main(String[] arg){
		read_file test=new read_file();
		HashMap<Byte,Integer> map=test.read("F:\\test\\test.txt");
		Iterator<Entry<Byte,Integer>> iter=map.entrySet().iterator();
		while(iter.hasNext()){
			Entry<Byte,Integer> e=iter.next();
			System.out.println(e.getKey()+"\t"+e.getValue());
		}
		
	}
}
